package fun.rockchen.service.concreteService;

import fun.rockchen.bean.MapTaskEntity;
import fun.rockchen.bean.User;
import fun.rockchen.bean.UserMapTag;
import fun.rockchen.dao.TagDao;
import fun.rockchen.dao.UserDao;
import fun.rockchen.service.interfaceService.UserService;
import fun.rockchen.utils.JwtUtil;
import fun.rockchen.utils.responseUtils.ResponseJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * @author Rock Chen
 * @Description:
 * @date 2023/10/15 19:30
 */

@Service
public class UserServiceImpl implements UserService {
    private final UserDao userDao;
    private final TagDao tagDao;

    @Autowired
    public UserServiceImpl(UserDao userDao, TagDao tagDao) {
        this.userDao = userDao;
        this.tagDao = tagDao;
    }


    @Override
    public ResponseEntity<?> getUserInfo(User user) {
        User userDB = userDao.getInfoByPhoneNum(user);
        if (userDB != null) {
            if (Objects.equals(user.getPassword(), userDB.getPassword())) {
                String id = userDB.getId();
                String userName = userDB.getUserName();
                String token = JwtUtil.sign(id, userName);
                if (token != null) {
                    userDB.setToken(token);
                    return ResponseEntity.ok(ResponseJson.LoginSuccess(userDB));
                }
            }
            return ResponseEntity.status(455).body(ResponseJson.AccountAndPasswordFail());
        } else {
            return ResponseEntity.status(444).body(ResponseJson.NoUser());
        }
    }


    @Override
    public ResponseEntity<?> addUserInfo(User user) {
        try {
            String id = UUID.randomUUID().toString().replace("-", "");
            user.setId(id);
            boolean flag = userDao.addUser(user);
            if (flag) {
                String userName = user.getUserName();
                String token = JwtUtil.sign(id, userName);
                if (token != null) {
                    user.setToken(token);
                }
            }
            return ResponseEntity.ok().body(ResponseJson.userCreateSuccess(user));
        } catch (Exception e) {
            return ResponseEntity.status(411).body(ResponseJson.userExist());
        }
    }

    @Override
    public ResponseEntity<?> deleteUser(MapTaskEntity mapTaskEntity) {
        boolean flag = userDao.deleteUser(mapTaskEntity);
        if (flag) {
            return ResponseEntity.ok().body(ResponseJson.deleteSuccess());
        } else {
            return ResponseEntity.status(466).body(ResponseJson.deleteFail());
        }
    }

    @Override
    public ResponseEntity<?> updateUserTag(MapTaskEntity mapTaskEntity) {
        boolean flag = userDao.updateUserTag(mapTaskEntity);
        if (flag) {
            return ResponseEntity.ok().body(ResponseJson.updateSuccess());
        } else {
            return ResponseEntity.status(477).body(ResponseJson.updateFail());
        }
    }

    @Override
    public ResponseEntity<?> getUserTag(String id) {
        List<String> tagList = userDao.getUserTag(id);
        if (!tagList.isEmpty()) {
            return ResponseEntity.ok(ResponseJson.getSuccess(tagList));
        } else {
            return ResponseEntity.ok(ResponseJson.NoData());
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackForClassName = "Exception")
    public ResponseEntity<?> addUserTag(UserMapTag userMapTag) {
        String tagId = UUID.randomUUID().toString().replace("-", "");
        userMapTag.setTagId(tagId);
        try {
            boolean tagFlag = tagDao.addTagStep1(userMapTag);
            if (tagFlag) {
                boolean tagFlag1 = tagDao.addTagStep2(userMapTag);
                if (tagFlag1) {
                    return ResponseEntity.ok(ResponseJson.insertSuccess());
                }
            }
            return ResponseEntity.status(488).body(ResponseJson.insertFail());
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseEntity.status(496).body(ResponseJson.fail());
        }
    }
}
